#include <iostream>
#include <vector>
#include <unordered_map>
#include <functional>
#include <string>

class EventDispatcher
{
public:
    using EventHandler = std::function<void(const std::string&)>;

    void addListener(const std::string& eventName, EventHandler handler) {
        listeners[eventName].push_back(handler);
    }

    void dispatchEvent(const std::string& eventName, const std::string& data) {
        if (listeners.find(eventName) != listeners.end())
        {
            for (auto handler : listeners[eventName])
            {
                handler(data);
            }
            
        }
        
    }
    
private:
    std::unordered_map<std::string, std::vector<EventHandler>> listeners;

};


int main() {
    EventDispatcher dispatcher;

    // 注册lambda表达式作为监听器
    dispatcher.addListener("click",[](const std::string& data){
        std::cout << "Click event with data : " << data << std::endl;
    });

    // 注册普通函数
    auto hoverHandler = [](const std::string& data) {
        std::cout << "Hover event : " << data << std::endl;
    };

    dispatcher.addListener("hover", hoverHandler);

    // 触发事件
    dispatcher.dispatchEvent("click", "Button1");
    dispatcher.dispatchEvent("hover", "Menu item");


    return 0;
}

